package Net;

import MainBoard.PVPMainBoard;

import java.io.*;
import java.net.*;

public class Server extends NetContainer {
    private ServerSocket serverForClient;
    private Socket socketOnServer;
    private String localHostIP;
    private boolean isConnecting;
    private PVPMainBoard pvpMainBoard;

    /**
     * 服务器的构造函数
     *
     * @param pvpMainBoard 游戏主界面
     * @throws UnknownHostException 本地IP获取异常
     */
    public Server(PVPMainBoard pvpMainBoard) throws UnknownHostException, SocketException {
        this.localHostIP = getLocalMachineInfo("IPv4 Address. . . . . . . . . . . :");
        this.pvpMainBoard = pvpMainBoard;
    }

    /**
     * 获取局域网本地IP
     *
     * @param str
     * @return 本地IP
     */
    static String getLocalMachineInfo(String str) {
        String line = "";
        int n;
        try {
            Process ps = Runtime.getRuntime().exec("cmd /c ipconfig /all");
            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            while (null != (line = br.readLine())) {
                if (line.equals("Wireless LAN adapter WLAN:")) {
                    while (null != (line = br.readLine())) {
                        if (line.contains(str)) {
                            n = line.indexOf(":");
                            line = line.substring(n + 2);
                            for (int i = 0; i < line.length(); i++) {
                                if (line.charAt(i) == '(') {
                                    line = line.substring(0, i);
                                    break;
                                }
                            }
                            return line;
                        }
                    }
                }
            }
            ps.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return line;
    }


    /**
     * 获取本地IP地址
     *
     * @return 本地的IP地址
     */
    public String getLocalHostIP() {
        InetAddress addr=null;
        try{
            addr=InetAddress.getLocalHost();
        }catch (UnknownHostException e){
            e.printStackTrace();
        }
        return addr.getHostAddress();
    }

    /**
     * 更改服务器连接状态
     *
     * @param connecting 服务器的连接状态
     */
    public void setConnecting(boolean connecting) {
        isConnecting = connecting;
    }

    /**
     * 初始化IO流
     *
     * @throws IOException
     */
    @Override
    public void initIO() throws IOException {
        in = new DataInputStream(socketOnServer.getInputStream());
        out = new DataOutputStream(socketOnServer.getOutputStream());
    }

    /**
     * 服务器运行函数
     */
    @Override
    public void run() {
        try {
            serverForClient = new ServerSocket(1921);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            socketOnServer = serverForClient.accept();
            initIO();
            isConnecting = true;
            if (isConnecting) {
                pvpMainBoard.getProcessLabel().setText("等待双方准备");
                pvpMainBoard.getButtonCreate().setVisible(false);
                pvpMainBoard.getButtonJoin().setVisible(false);
                pvpMainBoard.getButtonStart().setVisible(true);
                pvpMainBoard.getButtonBack().setVisible(true);
            }
            while (isConnecting) ;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("连接已断开");
            isConnecting = false;
        }
    }
}
